package com.gxuwz.medical.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.payRecord.PayRecord;
import com.mysql.jdbc.Statement;

public class PayRecordDao extends GenericDao<PayRecord> {

	@Override
	protected PayRecord handle(ResultSet rs) throws SQLException {
		try {
			// 实例化参合缴费记录对象
			PayRecord model = new PayRecord();
			// 取出ResultSet的值,存进PayRecord对象
			model.setChzNumber(rs.getString("chznumber"));
			model.setChzcode(rs.getString("chzcode"));
			model.setName(rs.getString("name"));
			model.setFamilycode(rs.getString("familycode"));
			model.setHousemaster(rs.getString("housemaster"));
			model.setHousecode(rs.getString("housecode"));
			model.setPayYear(rs.getString("payYear"));
			model.setPayAmount(rs.getDouble("payAmount"));
			model.setPayTime(rs.getDate("payTime"));
			model.setRegister(rs.getString("register"));
			//返回参合缴费对象
			return model;
		} catch (SQLException e) {
			throw new SQLException("结果集转为实例失败!", e);
		}
	}
	
	//计算指定人数的缴费总金额
	public double calAccount(int payNum, double payAmount) {
		double amount = 0.0;
		try {
			if(payNum !=0 && payAmount != 0) {
				amount = (payNum * payAmount);	
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return amount;
	}
	
	//获取指定家庭编号的当前年度参合缴费总人数

	public int getByFamily(String familycode, String payYear) throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int count = 0;
		try{
			conn = DbUtil.getConn();
			pstmt = conn.prepareStatement("select count(*) from t_payRecord where familycode=? and payYear=? ");
			int index = 1;
			pstmt.setString(index++, familycode);
			pstmt.setString(index++, payYear);
			// 执行查询操作
			rs = pstmt.executeQuery();
			if(rs.next()) {
				count = rs.getInt(1);
			}
			return count;
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(rs, pstmt, conn);
		}
	}
	
	/**
	 * 根据农合证号判断该农民是否参合
	 */
	public boolean isCanHe(String housecode){
		Connection conn = null;
		java.sql.Statement stmt = null;
		ResultSet rs = null;
		try{
			conn = DbUtil.getConn();
			String sql = "select familycode from t_payRecord where housecode='"+housecode+"'";
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				return true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
}
